package com.ming.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.yygh.enums.RefundStatusEnum;
import com.ming.yygh.model.order.PaymentInfo;
import com.ming.yygh.model.order.RefundInfo;
import com.ming.yygh.order.mapper.RefundInfoMapper;
import com.ming.yygh.order.service.RefundInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author Light
 * @date 2021/12/24 16:27
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        // 判断是否有重复数据的添加
        RefundInfo refundInfo = baseMapper.selectOne(new QueryWrapper<RefundInfo>()
                .eq("order_id", paymentInfo.getOrderId())
                .eq("payment_type", paymentInfo.getPaymentType()));
        if (!StringUtils.isEmpty(refundInfo)) {// 有相同数据
            return refundInfo;
        }
        // 添加记录
        refundInfo = new RefundInfo();
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
